package com.hyzx.qbasic.common.util;

import java.util.Map;

/**
 * Created by 黄江华 on 2019/1/3
 * 字符串变量占位符解析工具类
 */

public class StringParserUtils {

    public static String parseDollarMark(String text, Map<String, String> args) {
        return StringParserUtils.parse("${", "}", text, args);
    }

    public static String parseHashMark(String text, Map<String, String> args) {
        return StringParserUtils.parse("#{", "}", text, args);
    }

    public static String parseBraceMark(String text, Map<String, String> args) {
        return StringParserUtils.parse("{", "}", text, args);
    }

    public static String parse(String openToken, String closeToken, String text, Map<String, String> args) {
        if (text == null || text.isEmpty()) {
            return "";
        }

        if (args == null || args.size() == 0) {
            return text;
        }

        // 搜索变量占位符起始部分
        int start = text.indexOf(openToken, 0);
        if (start == -1) {
            return text;
        }
        char[] src = text.toCharArray();
        int offset = 0;
        final StringBuilder builder = new StringBuilder();
        StringBuilder expression = null;
        while (start > -1) {
            if (start > 0 && src[start - 1] == '\\') {
                // 如果变量占位符起始部分被\\转义，则移除转义符，并且不替换此次匹配
                builder.append(src, offset, start - offset - 1).append(openToken);
                offset = start + openToken.length();
            } else {
                // 搜索变量占位符结束部分
                if (expression == null) {
                    expression = new StringBuilder();
                } else {
                    expression.setLength(0);
                }
                builder.append(src, offset, start - offset);
                offset = start + openToken.length();
                int end = text.indexOf(closeToken, offset);
                while (end > -1) {
                    if (end > offset && src[end - 1] == '\\') {
                        // 如果变量占位符结束部分被\\转义，则移除转义符，继续匹配下一个变量占位符结束部分
                        expression.append(src, offset, end - offset - 1).append(closeToken);
                        offset = end + closeToken.length();
                        end = text.indexOf(closeToken, offset);
                    } else {
                        expression.append(src, offset, end - offset);
                        break;
                    }
                }
                if (end == -1) {
                    // 未搜索到变量占位符结束部分
                    builder.append(src, start, src.length - start);
                    offset = src.length;
                } else {
                    String argKey = expression.toString();
                    String argValue = args.get(argKey);
                    if (argValue != null && !argValue.isEmpty()) {
                        builder.append(argValue);
                    }
                    offset = end + closeToken.length();
                }
            }
            start = text.indexOf(openToken, offset);
        }
        if (offset < src.length) {
            builder.append(src, offset, src.length - offset);
        }
        return builder.toString();
    }
}
